package com.jiuyin.dianjing.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.model.TeamMember;
import com.jiuyin.dianjing.util.DisplayUtil;

import java.util.List;

public class ManagerGridAdapter extends RecyclerView.Adapter<ManagerGridAdapter.ViewHolder> {
    private List<TeamMember> mDataList;
    private Context mContext;
    private ItemClickListener itemClickListener;

    public ManagerGridAdapter(List<TeamMember> mDataList, Context mContext) {
        this.mDataList = mDataList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_managers, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position == mDataList.size()) {
            //加号
            holder.ivDelete.setVisibility(View.GONE);
            holder.ivHead.setImageResource(R.drawable.tianjiaguanli);
            holder.ivHead.setOnClickListener(v -> {
                if (itemClickListener != null) {
                    itemClickListener.onPlusClick();
                }
            });
        } else {
            holder.ivDelete.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(mDataList.get(position).header)
                    .override(DisplayUtil.dp2px(mContext, 46f))
                    .into(holder.ivHead);
            holder.ivHead.setOnClickListener(v -> {
                if (itemClickListener != null) {
                    itemClickListener.onManagerClick(position);
                }
            });
            holder.ivDelete.setOnClickListener(v -> {
                if (itemClickListener != null) {
                    itemClickListener.onRemoveClick();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size() == 0 ? 1 : mDataList.size() + 1;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onPlusClick();

        void onRemoveClick();

        void onManagerClick(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivHead;
        ImageView ivDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
    }
}
